package com.hd.crdao.activity;

import android.annotation.SuppressLint;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.view.ExpandHeadView;

import butterknife.BindView;

/**
 *
 *@author Programming
 *@date 2017/7/18 0018
 */

public class DivideFeildActivity extends BaseSecondActivity {


    public String param;

    @BindView(R.id.dividefeild_head)
    ExpandHeadView mDividefeildHead;
    @BindView(R.id.webview)
    WebView webView;


    @Override
    protected int setContentView() {
        return R.layout.activity_dividefeild;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mDividefeildHead;
    }

    @Override
    protected void initView() {
        param = getIntent().getStringExtra("param");
        setSettings(webView);
        webView.loadUrl("file:///android_asset/html/installment_plan.html");
        webView.addJavascriptInterface(new ProviderForJavaScript(), "connectedBridge");

    }

    @SuppressLint("SetJavaScriptEnabled")
    private void setSettings(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAppCacheEnabled(false);
        webView.setWebChromeClient(new WebChromeClient());
    }


    private class ProviderForJavaScript {
        @JavascriptInterface
        public void sendParam() {
            webView.post(new Runnable() {
                @Override
                public void run() {
                    ALog.d(param);
                    webView.loadUrl("javascript:vm.init(" + param + ")");
                }
            });
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.removeJavascriptInterface("connectedBridge");
            webView.destroy();
        }
    }
}
